/*******************************************************************************
 ** Name: common.cpp                                                          **
 ** Description:                         **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2011  Barry Gian James <gian@gamingods.net>            **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/
// $Id$


#include "common.hpp"
#include <string>
#include <stdlib.h>     // rand_r()
#include <cstdio>
#include <cstring>


__shared_ptr_null shared_null_ptr_t;


#if defined (USE_MEM_MGMT)

void * operator new(size_t size)
{
	return malloc(size);
}
// Placement new default
void * operator new(size_t, void *p)
{
	return p;
}

void * operator new [] (size_t size)
{
	return malloc(size);
}

void operator delete(void * ptr)
{
	if (ptr)
		free(ptr);
}

void operator delete [] (void * ptr)
{
	if (ptr)
		free(ptr);
}

void operator delete(void * p, void * const b)
{

}

#endif	/* defined(USE_MEM_MGMT) */

// @Deprecated
// The following string functions can be replaced with the Boost::StringAlgo
// alternatives.
std::string Uppercase(const std::string & str)
{
	std::string s = str;
	for (int i = 0; i < s.size(); i++)
		s[i] = std::toupper(s[i]);
	return s;
}

std::string Lowercase(const std::string & str)
{
	std::string s = str;
	for (int i = 0; i < s.size(); i++)
		s[i] = std::tolower(s[i]);
	return s;
}

std::string TrimWhiteSpace(const std::string & str)
{
	int wsf, wsb;
	wsf = str.find_first_not_of(WHITESPACE);
	wsb = str.find_last_not_of(WHITESPACE);
	if (wsf == std::string::npos) {
		wsf = 0;
		wsb = -1;
	}
	return str.substr(wsf,wsb - wsf + 1);
}

int Meters2Feet(int m)
{
    return (m*3.281);
}

int Feet2Meters(int f)
{
    return (f/3.281);
}

// Stolen from Tab.cpp @ COCO/R source by Hanspeter Moessenboeck
char    Hex2Char(const char* s) {
    int val = 0;
    int len = std::strlen(s);
    for (int i = 0; i < len; i++) {
        char ch = s[i];
        if ('0' <= ch && ch <= '9') val = 16 * val + (ch - '0');
        else if ('a' <= ch && ch <= 'f') val = 16 * val + (10 + ch - 'a');
        else if ('A' <= ch && ch <= 'F') val = 16 * val + (10 + ch - 'A');
        else {
            ;
        }
    }
    if (val >= 3) {/* pdt */
        ;
    }
    return (char) val;
}

char*   Char2Hex(const char ch) {
    char* format = new char[10];
    sprintf(format, "0x%04x", ch);
    return format;
}

